package com.aube.mis.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aube.mis.dto.ActorDTO;
import com.aube.mis.dto.ExtraConfigDTO;
import com.aube.mis.dto.ItemDTO;
import com.aube.mis.dto.PaginationResultDTO;
import com.aube.mis.dto.PicDTO;
import com.aube.mis.dto.PrizeDTO;
import com.aube.mis.dto.RelatedCallbackDTO;
import com.aube.mis.dto.VideoDTO;
import com.aube.mis.enums.ResourceType;
import com.aube.mis.service.ExtraConfigService;
import com.aube.mis.service.ResourceService;
import com.aube.mis.service.ShowSettingService;
import com.aube.mis.util.JsonUtil;
import com.aube.mis.vo.DataTableCriterias;

@Controller
@RequestMapping(value = "/resource")
public class ResourceController extends AbstractController {

	private static Logger log = LoggerFactory.getLogger(ResourceController.class);

	@Resource
	ResourceService resourceService;

	@Resource
	ShowSettingService showSettingService;

	@Resource
	ExtraConfigService extraConfigService;

	@RequestMapping(value = "/video/list")
	public String videoList(HttpServletResponse response, ModelMap map, String isSingleChoice, String isRelated, String callbackUrl, String relatedIds,
			@ModelAttribute RelatedCallbackDTO relatedCallbackDTO) throws Throwable {
		if ("1".equals(relatedCallbackDTO.getIsRelatedCallback())) {
			List<String> selectedRelatedIdList = new ArrayList<String>();
			if (StringUtils.isNotEmpty(relatedCallbackDTO.getSelectedRelatedIds())) {
				selectedRelatedIdList = Arrays.asList(relatedCallbackDTO.getSelectedRelatedIds().split(","));
			}
			if (relatedCallbackDTO.getRelatedType().equals("model")) {
				showSettingService.updateRelatedIdsByResource(getShowId(), selectedRelatedIdList, relatedCallbackDTO.getSourceId());
			} else {
				resourceService.relateResource(getShowId(), ResourceType.VIDEO.getCode(), relatedCallbackDTO.getSourceId(), relatedCallbackDTO.getRelatedType(), selectedRelatedIdList);
			}
		}
		map.put("relatedIds", relatedIds);
		map.put("callbackUrl", callbackUrl);
		map.put("isRelated", isRelated);
		map.put("resourceType", ResourceType.VIDEO.getCode());
		map.put("sourceId", relatedCallbackDTO.getSourceId());
		map.put("isSingleChoice", isSingleChoice);
		return "/resource/video/list";
	}

	@RequestMapping(value = "/video/list/data.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String videoListData(@ModelAttribute DataTableCriterias criterias) throws Throwable {
		PaginationResultDTO<VideoDTO> result = resourceService.getVideoList(criterias, getShowId());
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("list", result.getResultList());
		dataMap.put("total", result.getTotal());
		return this.createSuccessResponse(dataMap);
	}

	@RequestMapping(value = "/video/edit.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String videoEditAjax(@ModelAttribute VideoDTO videoDTO, @ModelAttribute ExtraConfigDTO extraConfigDTO) throws Throwable {
		videoDTO.setShowId(getShowId());
		VideoDTO result = resourceService.editVideo(videoDTO);
		if (extraConfigDTO != null && CollectionUtils.isNotEmpty(extraConfigDTO.getOptionList())) {
			extraConfigService.saveExtraConfig(getShowId(), result.getVideoId(), extraConfigDTO);
		}
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("video", result);
		return this.createSuccessResponse(dataMap);
	}

	@RequestMapping(value = "/video/delete.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String videoDelete(String videoId) throws Throwable {
		resourceService.deleteVideo(getShowId(), videoId);
		return this.createSuccessResponse();
	}

	@RequestMapping(value = "/video/edit", method = RequestMethod.GET)
	public String videoEdit(HttpServletResponse response, ModelMap map, String videoId) throws Throwable {
		if (StringUtils.isEmpty(videoId)) {
			map.put("isEdit", false);
		} else {
			map.put("isEdit", true);
			VideoDTO videoDetail = resourceService.getVideoDetail(getShowId(), videoId);
			map.put("video", videoDetail);
			map.put("videoId", videoDetail.getVideoId());
			ExtraConfigDTO extraConfig = extraConfigService.getExtraConfig(getShowId(), "video", videoId);
			map.put("selectedExtraConfig", extraConfig);
		}
		List<ExtraConfigDTO> extraConfigList = extraConfigService.getExtraConfigList(getShowId(), "video");
		map.put("extraConfigList", extraConfigList);
		return "/resource/video/edit.page";
	}

	@RequestMapping(value = "/video/preview")
	public String videoPreview(HttpServletResponse response, ModelMap map, String videoId) throws Throwable {
		VideoDTO videoDetail = resourceService.getVideoDetail(getShowId(), videoId);
		map.put("video", videoDetail);
		return "/resource/video/preview.page";
	}

	@RequestMapping(value = "/video/videoCode.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String getVideoCodeById(HttpServletResponse response, ModelMap map, String videoId, Double width, Double height) throws Throwable {
		String videoCode = resourceService.getVideoCodeById(getShowId(), videoId, width, height);
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("videoCode", videoCode);
		return this.createSuccessResponse(dataMap);
	}

	@RequestMapping(value = "/pic/list")
	public String picList(HttpServletResponse response, ModelMap map, String isSingleChoice, String isRelated, String callbackUrl, String relatedIds, String isRelatedCallback,
			String selectedRelatedIds, String relatedType, String sourceId) throws Throwable {
		if ("1".equals(isRelatedCallback)) {
			List<String> selectedRelatedIdList = new ArrayList<String>();
			if (StringUtils.isNotEmpty(selectedRelatedIds)) {
				selectedRelatedIdList = Arrays.asList(selectedRelatedIds.split(","));
			}
			if (relatedType.equals("model")) {
				showSettingService.updateRelatedIdsByResource(getShowId(), selectedRelatedIdList, sourceId);
			} else {
				resourceService.relateResource(getShowId(), ResourceType.STAGE_PHOTO.getCode(), sourceId, relatedType, selectedRelatedIdList);
			}
		}
		map.put("resourceType", ResourceType.STAGE_PHOTO.getCode());
		map.put("isRelated", isRelated);
		map.put("callbackUrl", callbackUrl);
		map.put("relatedIds", relatedIds);
		map.put("sourceId", sourceId);
		map.put("isSingleChoice", isSingleChoice);
		return "/resource/pic/list";
	}

	@RequestMapping(value = "/pic/list/data.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String picListData(@ModelAttribute DataTableCriterias criterias) throws Throwable {
		PaginationResultDTO<PicDTO> result = resourceService.getPicList(criterias, getShowId());
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("list", result.getResultList());
		dataMap.put("total", result.getTotal());
		return this.createSuccessResponse(dataMap);
	}

	@RequestMapping(value = "/pic/edit", method = RequestMethod.GET)
	public String picEdit(HttpServletResponse response, ModelMap map, String picId) throws Throwable {
		if (StringUtils.isEmpty(picId)) {
			map.put("isEdit", false);
		} else {
			PicDTO picDetail = resourceService.getPicDetail(getShowId(), picId);
			map.put("isEdit", true);
			map.put("pic", picDetail);
		}
		return "/resource/pic/edit.page";
	}

	@RequestMapping(value = "/pic/preview")
	public String picPreview(HttpServletResponse response, ModelMap map, String picId) throws Throwable {
		PicDTO picDetail = resourceService.getPicDetail(getShowId(), picId);
		map.put("pic", picDetail);
		return "/resource/pic/preview.page";
	}

	@RequestMapping(value = "/pic/edit.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String picEditAjax(@ModelAttribute PicDTO picDTO, @ModelAttribute ArrayList<String> aaa) throws Throwable {
		System.out.println(aaa);
		System.out.println(getRequest().getParameterMap());
		System.out.println(getRequest().getParameter("aaa"));
		System.out.println(JsonUtil.toJson(getRequest().getParameter("aaa")));
		picDTO.setShowId(getShowId());
		resourceService.editPic(picDTO);
		return this.createSuccessResponse();
	}

	@RequestMapping(value = "/pic/delete.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String picDelete(String picId) throws Throwable {
		resourceService.deletePic(getShowId(), picId);
		return this.createSuccessResponse();
	}

	@RequestMapping(value = "/actor/list")
	public String actorList(HttpServletResponse response, ModelMap map, String isSingleChoice, String isRelated, String callbackUrl, String relatedIds,
			@ModelAttribute RelatedCallbackDTO relatedCallbackDTO) throws Throwable {
		if ("1".equals(relatedCallbackDTO.getIsRelatedCallback())) {
			List<String> selectedRelatedIdList = new ArrayList<String>();
			if (StringUtils.isNotEmpty(relatedCallbackDTO.getSelectedRelatedIds())) {
				selectedRelatedIdList = Arrays.asList(relatedCallbackDTO.getSelectedRelatedIds().split(","));
			}
			if (relatedCallbackDTO.getRelatedType().equals("model")) {
				showSettingService.updateRelatedIdsByResource(getShowId(), selectedRelatedIdList, relatedCallbackDTO.getSourceId());
			} else {
				resourceService.relateResource(getShowId(), ResourceType.ACTOR.getCode(), relatedCallbackDTO.getSourceId(), relatedCallbackDTO.getRelatedType(), selectedRelatedIdList);
			}
		}
		map.put("resourceType", ResourceType.ACTOR.getCode());
		map.put("isRelated", isRelated);
		map.put("callbackUrl", callbackUrl);
		map.put("relatedIds", relatedIds);
		map.put("sourceId", relatedCallbackDTO.getSourceId());
		map.put("isSingleChoice", isSingleChoice);
		return "/resource/actor/list";
	}

	@RequestMapping(value = "/actor/list/data.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String actorListData(@ModelAttribute DataTableCriterias criterias) throws Throwable {
		PaginationResultDTO<ActorDTO> result = resourceService.getActorList(criterias, getShowId());
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("list", result.getResultList());
		dataMap.put("total", result.getTotal());
		return this.createSuccessResponse(dataMap);
	}

	@RequestMapping(value = "/actor/delete.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String actorDelete(String actorId) throws Throwable {
		resourceService.deleteActor(getShowId(), actorId);
		return this.createSuccessResponse();
	}

	@RequestMapping(value = "/actor/edit", method = RequestMethod.GET)
	public String actorEdit(HttpServletResponse response, ModelMap map, String actorId) throws Throwable {
		if (StringUtils.isEmpty(actorId)) {
			map.put("isEdit", false);
		} else {
			ActorDTO actorDetail = resourceService.getActorDetail(getShowId(), actorId);
			ExtraConfigDTO extraConfig = extraConfigService.getExtraConfig(getShowId(), "actor", actorId);
			map.put("selectedExtraConfig", extraConfig);
			map.put("isEdit", true);
			map.put("actor", actorDetail);
		}
		List<ExtraConfigDTO> extraConfigList = extraConfigService.getExtraConfigList(getShowId(), "actor");
		map.put("extraConfigList", extraConfigList);
		return "/resource/actor/edit.page";
	}

	@RequestMapping(value = "/actor/edit.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String actorEditAjax(@ModelAttribute ActorDTO actorDTO, @ModelAttribute ExtraConfigDTO extraConfigDTO) throws Throwable {
		if ("".equals(actorDTO.getIsNotClick())) {
			actorDTO.setIsNotClick("Y");
		} else {
			actorDTO.setIsNotClick("N");
		}
		actorDTO.setShowId(getShowId());
		ActorDTO actor = resourceService.editActor(actorDTO);
		if (extraConfigDTO != null && CollectionUtils.isNotEmpty(extraConfigDTO.getOptionList())) {
			extraConfigService.saveExtraConfig(getShowId(), actor.getActorId(), extraConfigDTO);
		}
		return this.createSuccessResponse();
	}

	@RequestMapping(value = "/getRelatedResourceIds.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String getRelatedResourceIds(String orignResourceType, String orignResourceId, String targetResourceType) throws Throwable {
		List<String> relatedResourceIdList = resourceService.getRelatedResourceIds(getShowId(), orignResourceType, orignResourceId, targetResourceType);
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("relatedResourceIds", relatedResourceIdList.toString().replaceAll("[\\[.\\].\\s+]", ""));
		return this.createSuccessResponse(dataMap);
	}

	@RequestMapping(value = "/actor/preview")
	public String actorPreview(HttpServletResponse response, ModelMap map, String actorId) throws Throwable {
		ActorDTO actorDetail = resourceService.getActorDetail(getShowId(), actorId);
		map.put("actor", actorDetail);
		return "/resource/actor/preview.page";
	}

	@RequestMapping(value = "/relate")
	public String relate(HttpServletResponse response, ModelMap map, String resourceType, String resourceId) throws Throwable {
		map.put("resourceType", resourceType);
		map.put("resourceId", resourceId);
		map.put("showId", getShowId());
		return "/resource/relate.page";
	}

	@RequestMapping(value = "/item/list")
	public String itemList(HttpServletResponse response, ModelMap map, String isSingleChoice, String isRelated, String callbackUrl, String relatedIds,
			@ModelAttribute RelatedCallbackDTO relatedCallbackDTO) throws Throwable {
		if ("1".equals(relatedCallbackDTO.getIsRelatedCallback())) {
			List<String> selectedRelatedIdList = new ArrayList<String>();
			if (StringUtils.isNotEmpty(relatedCallbackDTO.getSelectedRelatedIds())) {
				selectedRelatedIdList = Arrays.asList(relatedCallbackDTO.getSelectedRelatedIds().split(","));
			}
			if (relatedCallbackDTO.getRelatedType().equals("model")) {
				showSettingService.updateRelatedIdsByResource(getShowId(), selectedRelatedIdList, relatedCallbackDTO.getSourceId());
			} else {
				resourceService.relateResource(getShowId(), ResourceType.ITEM.getCode(), relatedCallbackDTO.getSourceId(), relatedCallbackDTO.getRelatedType(), selectedRelatedIdList);
			}
		}
		map.put("resourceType", ResourceType.ITEM.getCode());
		map.put("isRelated", isRelated);
		map.put("callbackUrl", callbackUrl);
		map.put("relatedIds", relatedIds);
		map.put("sourceId", relatedCallbackDTO.getSourceId());
		map.put("isSingleChoice", isSingleChoice);
		return "/resource/item/list";
	}

	@RequestMapping(value = "/item/list/data.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String itemListData(@ModelAttribute DataTableCriterias criterias) throws Throwable {
		PaginationResultDTO<ItemDTO> result = resourceService.getItemList(criterias, getShowId());
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("list", result.getResultList());
		dataMap.put("total", result.getTotal());
		return this.createSuccessResponse(dataMap);
	}

	@RequestMapping(value = "/item/delete.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String itemDelete(String itemId) throws Throwable {
		resourceService.deleteItem(getShowId(), itemId);
		return this.createSuccessResponse();
	}

	@RequestMapping(value = "/item/edit", method = RequestMethod.GET)
	public String itemEdit(HttpServletResponse response, ModelMap map, String itemId) throws Throwable {
		if (StringUtils.isEmpty(itemId)) {
			map.put("isEdit", false);
		} else {
			ItemDTO itemDetail = resourceService.getItemDetail(getShowId(), itemId);
			map.put("isEdit", true);
			map.put("item", itemDetail);
		}
		return "/resource/item/edit.page";
	}

	@RequestMapping(value = "/item/edit.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String itemEditAjax(@ModelAttribute ItemDTO itemDTO) throws Throwable {
		itemDTO.setShowId(getShowId());
		resourceService.editItem(itemDTO);
		return this.createSuccessResponse();
	}

	@RequestMapping(value = "/item/detail.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String itemDetailAjax(String itemId) throws Throwable {
		ItemDTO itemDTO = resourceService.getItemDetail(getShowId(), itemId);
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("item", itemDTO);
		return this.createSuccessResponse(dataMap);
	}

	@RequestMapping(value = "/item/preview")
	public String itemPreview(HttpServletResponse response, ModelMap map, String itemId) throws Throwable {
		ItemDTO itemDetail = resourceService.getItemDetail(getShowId(), itemId);
		map.put("item", itemDetail);
		return "/resource/item/preview.page";
	}

	@RequestMapping(value = "/item/select", method = RequestMethod.GET)
	public String itemSelect(HttpServletResponse response, ModelMap map) throws Throwable {
		DataTableCriterias criterias = new DataTableCriterias(0, 9999);
		PaginationResultDTO<ItemDTO> itemList = resourceService.getItemList(criterias, getShowId());
		map.put("itemList", itemList.getResultList());
		return "/resource/item/select.page";
	}

	@RequestMapping(value = "/prize/list")
	public String prizeList(HttpServletResponse response, ModelMap map, String isSingleChoice, String isRelated, String callbackUrl, String relatedIds,
			@ModelAttribute RelatedCallbackDTO relatedCallbackDTO) throws Throwable {
		map.put("resourceType", ResourceType.PRIZE.getCode());
		map.put("isRelated", isRelated);
		map.put("callbackUrl", callbackUrl);
		map.put("relatedIds", relatedIds);
		map.put("sourceId", relatedCallbackDTO.getSourceId());
		map.put("isSingleChoice", isSingleChoice);
		return "/resource/prize/list";
	}

	@RequestMapping(value = "/prize/list/data.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String prizeListData(@ModelAttribute DataTableCriterias criterias) throws Throwable {
		PaginationResultDTO<PrizeDTO> result = resourceService.getPrizeList(criterias, getShowId());
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("list", result.getResultList());
		dataMap.put("total", result.getTotal());
		return this.createSuccessResponse(dataMap);
	}

	@RequestMapping(value = "/prize/delete.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String prizeDelete(String prizeId) throws Throwable {
		resourceService.deletePrize(prizeId);
		return this.createSuccessResponse();
	}

	@RequestMapping(value = "/prize/edit", method = RequestMethod.GET)
	public String prizeEdit(HttpServletResponse response, ModelMap map, String prizeId) throws Throwable {
		if (StringUtils.isEmpty(prizeId)) {
			map.put("isEdit", false);
		} else {
			PrizeDTO prizeDetail = resourceService.getPrizeDetail(prizeId);
			map.put("isEdit", true);
			map.put("prize", prizeDetail);
		}
		return "/resource/prize/edit.page";
	}

	@RequestMapping(value = "/prize/edit.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String prizeEditAjax(@ModelAttribute PrizeDTO prizeDTO) throws Throwable {
		prizeDTO.setShowId(getShowId());
		resourceService.editPrize(prizeDTO);
		return this.createSuccessResponse();
	}

	@RequestMapping(value = "/prize/stock", method = RequestMethod.GET)
	public String prizeStock(HttpServletResponse response, ModelMap map, String prizeId) throws Throwable {
		PrizeDTO prizeDetail = resourceService.getPrizeDetail(prizeId);
		map.put("prize", prizeDetail);
		return "/resource/prize/stock.page";
	}

	@RequestMapping(value = "/prize/addStock.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String addStock(String prizeId, Integer addStock) throws Throwable {
		resourceService.addPrizeStock(prizeId, addStock);
		return this.createSuccessResponse();
	}

	@RequestMapping(value = "/prize/select", method = RequestMethod.GET)
	public String prizeSelect(HttpServletResponse response, ModelMap map, String sourceId) throws Throwable {
		DataTableCriterias criterias = new DataTableCriterias(0, 9999);
		PaginationResultDTO<PrizeDTO> prizeList = resourceService.getPrizeList(criterias, getShowId());
		map.put("prizeList", prizeList.getResultList());
		map.put("sourceId", sourceId);
		return "/resource/prize/select.page";
	}

}
